<?php

/**
 * Description of Application_Model_Fields_File
 *
 * @author Jean Brito
 */
class Application_Model_Fields_File extends Application_Model_Fields_Abstract{
	//protected $_sqlType='longblob';
	//protected $_sqlSize=null;

	protected $_url='data/uploads';
	public function __construct($model,$setup){
		parent::__construct($model,$setup);
		if(isset($setup['url']))$this->_url=$setup['url'];
	}

	public function setValue($value='',$row=array()){
		$value=trim($value);
		if(isset($_FILES[$this->getName()])){

			if(empty($value['name']))return $value;
			if($value['name']==' ')return $value;
			$fd=explode('/',trim($this->_url.'/'.$this->getModel()->getTableName().'/'.$this->_name,"/"));
			$file="./";
			foreach($fd as $d=>$dir){
				$file.=$dir;
				if(!is_dir($file)&&$d<(count($fd))){
					mkdir($file);
				}
				$file.="/";
			}

			$fo=fopen($file.$value['name'],'w');
			fwrite($fo,file_get_contents($value['tmp_name']));
			fclose($fo);

			$value=$file.$value['name'];
		}
		return $value;
	}



}
